﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Net; 
public static class SendMsg
{ 
    //调用时只需要把拼成的URL传给该函数即可。判断返回值即可

    public static string Uid = "mimikeji";
    public static string key = "58782de9ba92e0222aa2faf8e0172f69";
    public static string GetHtmlFromUrl(string url)
    {
        string strRet = null;

        if (url == null || url.Trim().ToString() == "")
        {
            return strRet;
        }
        string targeturl = url.Trim().ToString();
        try
        {
            HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
            hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
            hr.Method = "Get";
            hr.Timeout = 30 * 60 * 1000;
            WebResponse hs = hr.GetResponse();
            Stream sr = hs.GetResponseStream();
            StreamReader ser = new StreamReader(sr, Encoding.Default);
            strRet = ser.ReadToEnd();
        }
        catch (Exception ex)
        {
            strRet = null;
        }
        return strRet;
    }

    public static string HttpPost(string Url, string postDataStr)
    {
        string res = string.Empty;
        byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
        // Console.Write(Encoding.UTF8.GetString(dataArray));

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataArray.Length;
        //request.CookieContainer = cookie;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(dataArray, 0, dataArray.Length);
        dataStream.Close();
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            res = reader.ReadToEnd();
            reader.Close();
            Console.Write("\nResponse Content:\n" + res + "\n");
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //res = reader.ReadToEnd();
            //reader.Close();
            //Console.Write("\nResponse Content:\n" + res + "\n");

        }
        catch (Exception e)
        {
            Console.Write(e.Message + e.ToString());
        }
        return res;
    }
}